package com.jarvis.hospital.util;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by jarvis.liu on 2016-10-25.
 */

public class AddressManager {


    private static Properties properties;

    private static AddressManager instance = null;

    // host
    private static String host = "";

    private AddressManager(Context context) {
        InputStream is = null;
        try {
            is = JarvisUtil.APP.getAssets().open("config/address.properties");
            properties = new Properties();
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            Util.closeStream(is);
        }
    }

    /**
     * 实例化<br>
     * 该方法在系统启动的时候调用
     *
     * @param context context
     * @return instance
     */
    public static AddressManager getInstance(Context context) {
        if (instance == null) {
            instance = new AddressManager(context);
        }
        return instance;
    }

    /**
     * 取值<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key the key
     * @param defaultValue defaultValue
     * @return the value of the key
     */
    public static String get(String key, String defaultValue) {
        return properties.getProperty(key, defaultValue);
    }

    /**
     * 取值<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key the key
     * @return the value of the key
     */
    public static String get(String key) {
        return properties.getProperty(key, "");
    }

    /**
     * 获取完整的URL地址<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key the key
     * @param defaultValue defaultValue
     * @return the value of the key
     */
    public static String getUrl(String key, String defaultValue) {
        return getHost() + get(key, defaultValue);
    }

    /**
     * 获取完整的URL地址<br>
     * 输入的参数为raw/address.properties中的key
     *
     * @param key the key
     * @return the value of the key
     */
    public static String getUrl(String key) {
        return getHost() + properties.getProperty(key, "");
    }

    /**
     * 获取主机<br>
     * 获取主机地址
     *
     * @return the host of api address
     */
    public static String getHost() {
//        if (TextUtils.isEmpty(host)) {
        host = properties.getProperty("host", "");
//        }
        return host;
    }
    public static String getVersion() {
//        if (TextUtils.isEmpty(host)) {
        host = properties.getProperty("version", "");
//        }
        return host;
    }

    public static String getFileHost() {
//        if (TextUtils.isEmpty(host)) {
        host = properties.getProperty("file_host", "");
//        }
        return host;
    }
}
